--[[
	Galactica
	File: init.lua
]]

Galactica = {}
Galactica.Players = {}
Galactica.Version = 1.0

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("config.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
include("saveload.lua")

function Galactica.NofityAll(Text)
	for k, ply in pairs(player.GetAll()) do
		ply:SendLua("GAMEMODE:AddNotify(\""..Text.."\", NOTIFY_GENERIC, 5); surface.PlaySound(\"ambient/water/drip"..math.random(1, 4)..".wav\")")
		ply:PrintMessage(HUD_PRINTCONSOLE, Text)
	end
	Msg(Text.."\n")
end

function Galactica.Nofity(ply, Text)
	if(Galactica.Players[ply:SteamID()].LastNotify >= CurTime() - 1) then return end
	ply:SendLua("GAMEMODE:AddNotify(\""..Text.."\", NOTIFY_GENERIC, 5); surface.PlaySound(\"ambient/water/drip"..math.random(1, 4)..".wav\")")
	ply:PrintMessage(HUD_PRINTCONSOLE, Text)
	Galactica.Players[ply:SteamID()].LastNotify = CurTime()
end

function Galactica.ChatPrint(ply, Text)
	ply:PrintMessage(HUD_PRINTTALK, Text)
end

function GM:PlayerSpawn(ply)
	ply:SetCollisionGroup(11)
	if(ply:IsAdmin() == true) then
		self.BaseClass:PlayerLoadout(ply)
	else
		for k, v in pairs(Galactica_SpawnWeapons) do
			ply:Give(v)
		end
	end
end

function GM:PlayerInitialSpawn(ply)
	if(Galactica.LoadData(ply) == true) then
		Galactica.ChatPrint(ply, "Your profile has been loaded. Welcome back "..ply:Nick())
	else
		Galactica.ChatPrint(ply, "A profile for you has been created. Welcome "..ply:Nick())
		Galactica.Players[ply:SteamID()] = {}
		Galactica.Players[ply:SteamID()].Money = "25"
		Galactica.Players[ply:SteamID()].Stools = {}
		Galactica.Players[ply:SteamID()].LastNotify = CurTime()
	end
	Galactica.DataUpdate(ply)
	timer.Simple(60, Galactica.DataUpdateTimer, ply)
end


function Galactica.GetMoney(ply)
	return tonumber(Galactica.Players[ply:SteamID()].Money)
end

function Galactica.SetMoney(ply, amount)
	Galactica.Players[ply:SteamID()].Money = amount
end

function Galactica.DataUpdate(ply)
	umsg.Start("GalacticaDataUpdateClient", ply)
	umsg.Short(Galactica.GetMoney(ply))
	umsg.Short(Galactica.Version)
	umsg.End()
	Galactica.SaveData(ply)
end

function Galactica.DataUpdateTimer(ply)
	if(ply:IsValid()) then
		if(Galactica.AfkTimer.GetAFK(ply) == false) then
			Galactica.SetMoney(ply, Galactica.GetMoney(ply) + 1)
		end
		Galactica.DataUpdate(ply)
		timer.Simple(60, Galactica.DataUpdateTimer, ply)
	end
end

function GM:ShowHelp(ply)
	Galactica.Nofity(ply, "Galactica Version "..Galactica.Version.." coded by Spacetech/Tomato3017")
end

function GM:ShowTeam(ply)
	Galactica.Nofity(ply, "Using a super secret prop protection")
end

function GM:ShowSpare1(ply)
	self:ShowHelp(ply)
end

function GM:ShowSpare2(ply)
	self:ShowTeam(ply)
end

function GM:CanTool(ply, tr, stool)
	if(table.HasValue(Galactica_RestrictedStools, stool) and ply:IsAdmin() == false) then
		Galactica.Nofity(ply, "This toolgun is restricted")
		return false
	else
		return true
	end
end

Msg("================================\n")
Msg("Galactica Version "..Galactica.Version.."\n")
Msg("Loading Modules...\n")
local Files = file.FindInLua("../gamemodes/Galactica/gamemode/modules/*.lua") 
for _, File in pairs(Files) do 
	Msg("Loading "..File.."...")
	include("modules/"..File)
	Msg("Loaded Successfully\n")
end
Msg("Loaded Modules Successfully\n")
Msg("================================\n")
